<?php

/**
 * 关联版式
 *
 * @author birdy <freebirdy@foxmail.com>
 * @link http://www.aiyuangong.com
 */

namespace app\seller\controller;

class Plate extends \app\common\controller\Seller
{

    /**
     * 相册
     */
    public function index()
    {
        // 版式列表
        $map = [
            'store_id' => $this->store_id
        ];

        $p_name = input('p_name', '', 'trim');
        $p_position = input('p_position/d', -1);
        if ($p_name) {
            $map['plate_name'] = array('like', '%'.$p_name.'%');
        }
        if (in_array($p_position, array('0','1'))) {
            $map['plate_position'] = $p_position;
        }
        
        $lists = $this->db_lists_page('store_plate', $map, 10);
        $this->assign("lists", $lists);
        $this->assign('left_nav', 'plate');
        return view();
    }
    
    /**
     * 添加/编辑
     */
    public function modify()
    {
        if ($this->request->isPost()) {
            $param = array();
            $param['plate_position'] = input('post.p_position/d', 1) ? 1 : 0;
            $param['plate_name']     = mb_substr(input('post.p_name', '', 'htmlspecialchars'), 0, 12);
            $param['plate_content']  = input('post.p_content', '', 'htmlspecialchars');
            
            $id = input('post.p_id/d', 0);
            if ($id > 0) {
                $map = [
                    'store_id' => $this->store_id,
                    'plate_id' => $id
                ];
                $rs =rpc("common/common/update", ['store_plate', $map, $param]);
                $tip = '编辑';
            } else {
                $param['store_id'] = $this->store_id;
                $rs = rpc("common/common/create", ['store_plate', $param]);
                $tip = '添加';
            }
            
            if ($rs) {
                showDialog('关联版式'.$tip.'成功', url('seller/plate/index'), 'succ', $this->request->isAjax() ? 'CUR_DIALOG.close();' : '');
            } else {
                showDialog('关联版式'.$tip.'失败');
            }
        }
        
        $id = input('p_id/d', 0);
        if ($id > 0) {
            $map = [
                'store_id' => $this->store_id,
                'plate_id' => $id
            ];
            $item = rpc('common/common/one', ['store_plate', $map]);
            $tip  = '编辑';
        } else {
            $item = ['plate_position' => 1];
            $tip  = '添加';
        }
        $this->assign('item', $item);
        $this->assign('left_nav', 'plate');
        return view();
    }
    
    /**
     * 删除
     */
    public function drop()
    {
        $id = explode(",", trim(input('p_id')));
        !$id && showDialog('图片删除失败');

        $map = [
            'store_id' => $this->store_id,
            'plate_id' => ['in', $id]
        ];

        if (rpc("common/common/delete", ['store_plate', $map])) {
            showDialog('删除成功', 'reload', 'succ');
        }
        showDialog('删除失败');
    }
}